package com.qianfeng.zhangwei.windowmanager;

import android.content.Context;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;

/**
 * @说 明：
 * @项目名称：Sz1504Adv
 * @包 名： com.qianfeng.zhangwei.windowmanager
 * @类 名： WindowManagerUtils
 * @创 建人： zhangwei
 * @创建时间：2015-08-12 15:33
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注：
 */
public class WindowManagerUtils {
    private static WindowManagerUtils windowManagerUtils;
    private WindowManager windowManager;
    private WindowManager.LayoutParams params;
    private FloatView floatView;
    private static Context context;

    public static void init(Context context) {
        WindowManagerUtils.context = context;
    }

    private WindowManagerUtils() {
        if (context != null) {
            getWindowManager();
            floatView = new FloatView(context);
        } else {
            Log.e("------>>>", "请先调用init方法初始化工具类");
        }
    }

    public static WindowManagerUtils getInstance() {
        if (windowManagerUtils == null) {
            windowManagerUtils = new WindowManagerUtils();
        }
        return windowManagerUtils;
    }

    public void show() {
        params = new WindowManager.LayoutParams();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.x = 200;
        params.y = 200;
        params.type = WindowManager.LayoutParams.TYPE_PHONE;//声明wm的显示权限
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        params.format = PixelFormat.RGBA_8888;//设置windowmanager背景透明
        params.gravity = Gravity.TOP | Gravity.LEFT;
        floatView.setParams(params);
        getWindowManager().addView(floatView, params);
    }

    public WindowManager getWindowManager() {
        if (windowManager == null) {
            windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        return windowManager;

    }


    public void cancel() {
        if (windowManager != null) {
            windowManager.removeView(floatView);
            windowManager = null;
        }
    }

}
